package com.pyip.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pyip.reggie.Entity.Category;
import com.pyip.reggie.Entity.Dish;
import com.pyip.reggie.Entity.Setmeal;
import com.pyip.reggie.Entity.User;
import com.pyip.reggie.common.CustomException;
import com.pyip.reggie.common.R;
import com.pyip.reggie.mapper.CategoryMapper;
import com.pyip.reggie.mapper.UserMapper;
import com.pyip.reggie.service.CategoryService;
import com.pyip.reggie.service.DishService;
import com.pyip.reggie.service.SetmealService;
import com.pyip.reggie.service.UserService;
import com.pyip.reggie.utils.ValidateCodeUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Slf4j
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    // 缓存验证码
    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public R<String> sendMessage(User user, HttpSession session) {
        String phone = user.getPhone();
        if(!phone.isEmpty()){
            String code = ValidateCodeUtils.generateValidateCode(4).toString();
            log.info("code is {}",code);
            //调用阿里云提供的短信服务API完成发送短信
            //SMSUtils.sendMessage("瑞吉外卖","",phone,code);
//            session.setAttribute(phone,code);
            // 添加5分钟的验证码过期时间
            redisTemplate.opsForValue().set(phone,code,5, TimeUnit.MINUTES);
            return R.success("手机验证码发送成功");
        }
        return R.success("手机验证码发送失败");
    }

    @Override
    public R<User> login(Map<String,String> map, HttpSession session) {
        log.info((String) map.get("phone"));
        String phone = map.get("phone").toString();
        String code = map.get("code").toString();
        // 从redisTemplate中获取验证码
        Object o = redisTemplate.opsForValue().get(phone);

//        String sessionCode = session.getAttribute(phone) == null ? "" : session.getAttribute(phone).toString();
        // 改为从Redis中获取
        String sessionCode = o == null ? "" : o.toString();

        if(sessionCode.equals(code)){
            // 登陆成功
            LambdaQueryWrapper<User> ulqw = new LambdaQueryWrapper<>();
            ulqw.eq(User::getPhone,phone);
            User user = baseMapper.selectOne(ulqw);
            if(user == null){
                // 用户不存在，要添加用户
                user = new User();
                user.setPhone(phone);
                user.setStatus(1);
                baseMapper.insert(user);
                // 如果是新添加，则原user中没有内容（id)，要把原user给覆盖
                user = baseMapper.selectOne(ulqw);
            }
            session.setAttribute("user",user.getId());
            // 登陆成功，则删除验证码
            redisTemplate.delete(phone);
            return R.success(user);
        }

        return R.error("登陆失败");
    }
}
